# 访问必应搜索：https://cn.bing.com/?mkt=zh-CN
# 点击搜索按钮，输入关键字
# 点击搜索按钮
# 获取当前句柄
# 点击第一个标题
# 切换窗口
# 断言标题

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

def work_bing():
  driver=webdriver.Chrome()
  driver.get("https://cn.bing.com/?mkt=zh-CN")
  time.sleep(5)
  ele_input=driver.find_element(By.ID,"sb_form_q")
  ele_input.send_keys("测试人社区")
  ele_input.send_keys(Keys.ENTER)
  # driver.find_element(By.ID,"search_icon").click()


  #句柄的获取
  handles=driver.window_handles
  print(handles)
  
  ele=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="b_results"]/li[1]/h2')))
  ele.click()

  #切换窗口
  driver.switch_to.window(handles[-1])
  time.sleep(5)
  assert "111社区" in ele.text

if __name__ == '__main__':
  work_bing()